package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.component.SwaggerForm;
import com.github.aloxc.plugin.restplus.entity.EnvironmentConfig;
import com.github.aloxc.plugin.restplus.entity.RestConfig;
import com.github.aloxc.plugin.restplus.entity.SwaggerConfig;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.swagger.SwaggerAdapter;
import com.github.aloxc.plugin.restplus.utils.JsonUtils;
import com.github.aloxc.plugin.restplus.utils.Log;
import com.intellij.openapi.ui.Messages;
import org.apache.commons.io.FileUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;

/**
 * 导入swagger
 */
public class SwaggerFileListener implements ActionListener {
    private SwaggerForm swaggerForm;

    public SwaggerFileListener(SwaggerForm swaggerForm) {

        this.swaggerForm = swaggerForm;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //                FileNameExtensionFilter filter = new FileNameExtensionFilter("swagger api");
//                fileChooser.setFileFilter(filter);
        int i = swaggerForm.getFileChooser().showOpenDialog(swaggerForm);// 显示文件选择对话框
        // 判断用户单击的是否为“打开”按钮
        if (i == JFileChooser.APPROVE_OPTION) {
            File selectedFile = swaggerForm.getFileChooser().getSelectedFile();// 获得选中的文件对象
            int result = JOptionPane.showConfirmDialog(
                    swaggerForm,
                    I18Bundle.getMessage("com.github.aloxc.plugin.restplus.swagger.tips.info"),
                    I18Bundle.getMessage("com.github.aloxc.plugin.restplus.swagger.tips.title"),
                    JOptionPane.YES_NO_CANCEL_OPTION
            );
            swaggerForm.getProgressBar().setText("");
            switch (result) {
                case JOptionPane.YES_OPTION:
                    swaggerForm.getFileAddress().setText(selectedFile.getAbsolutePath());// 显示选中文件的名称
                    swaggerForm.getProgressBar().setForeground(Consts.MAIN_COLOR);
                    swaggerForm.getProgressBar().setVisible(true);
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                String content = FileUtils.readFileToString(new File(selectedFile.getAbsolutePath()), "UTF-8");
                                //Log.log("SwaggerForm", content);
                                //Log.log("SwaggerForm", "aa");
                                HashMap json = (HashMap) JsonUtils.toBean(content, Object.class);
                                //Log.log("SwaggerForm", "bb");
                                HashMap info = (HashMap) json.get("info");
                                //Log.log("SwaggerForm", "cc");
                                String version = (String) json.get("swagger");
                                //Log.log("SwaggerForm", "dd");
                                SwaggerAdapter swaggerAdapter = SwaggerAdapter.getInstance(version, swaggerForm.getProject());
                                //Log.log("SwaggerForm", "ee");
                                if (swaggerAdapter == null) {
                                    swaggerForm.getProgressBar().setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.swagger.tips.unsupported.version.text"));
                                    return;
                                }
                                //Log.log("SwaggerForm", "ff");
                                EnvironmentConfig environmentConfig = swaggerAdapter.getEnvironmentConfig(json);
                                //Log.log("SwaggerForm", "gg");
                                SwaggerConfig swaggerConfig = swaggerAdapter.getApiInfo(json);
                                //Log.log("SwaggerForm", "hh");
                                RestConfig restConfig = RestConfig.loadConfig(swaggerForm.getProject());
                                //Log.log("SwaggerForm", "ii");
                                SwaggerConfig find = restConfig.getSwaggerConfigList().stream().filter(it -> it.getName().equals(swaggerConfig.getName())).findFirst().orElse(swaggerConfig);
                                //Log.log("SwaggerForm", "jj");
                                restConfig.getSwaggerConfigList().remove(find);
                                //Log.log("SwaggerForm", "kk");
                                restConfig.getSwaggerConfigList().add(swaggerConfig);
                                //Log.log("SwaggerForm", "ll");
                                restConfig.getEnv().remove(environmentConfig.getName());
                                //Log.log("SwaggerForm", "mm");
                                restConfig.getEnv().put(environmentConfig.getName(), environmentConfig);
                                //Log.log("SwaggerForm", "nn");
                                restConfig.save(swaggerForm.getProject());
                                new RefreshListener(swaggerForm.getProject()).doRefreshProject();
                                swaggerForm.getProgressBar().setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.swagger.tips.finish"));
                            } catch (Throwable e1) {
                                StringWriter result1 = new StringWriter();
                                PrintWriter printWriter = new PrintWriter(result1);
                                e1.printStackTrace(printWriter);
                                Messages.showMessageDialog(result1.toString(), "Has exception in importing swagger!", null);
                            }
                        }
                    }.start();
                    break;
                case JOptionPane.CANCEL_OPTION:
                case JOptionPane.NO_OPTION:
                default:
                    break;
            }
        }
    }
}
